﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ManageUsersSelected : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadSelectedUsers();
        }
    }
   
    private void LoadSelectedUsers()
    {
        
        ListItem lst;
        
        foreach (StudentInfo user in UserSelection.sObj)
        {
            lst = new ListItem();
            lst.Text = user.sname;
            lst.Value = user.sid.ToString();
            chklstSelectedUsers.Items.Add(lst);
           
        }
        //UserSelection.sObj.Clear();
        for (int i = 0; i < chklstSelectedUsers.Items.Count; i++)
        {
            chklstSelectedUsers.Items[i].Selected = true;

        }
    }
    
    protected void btnApply_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <chklstSelectedUsers.Items.Count; i++)
        {
            if (chklstSelectedUsers.Items[i].Selected!=true)
            {
                unsubscribeUser(chklstSelectedUsers.Items[i].Value);
                chklstSelectedUsers.Items.RemoveAt(i);
                
                i--;
            }
        }
        
    }

    private void unsubscribeUser(object userId)
    {
        string user=userId.ToString();
        for (int i = 0; i < UserSelection.sObj.Count; i++)
        {
            string id=UserSelection.sObj[i].sid.ToString();
            if (id==user)
            {
                UserSelection.sObj.Remove(UserSelection.sObj[i]);
            }
        }
    }
}